1394A - Boboniu Chats with Du - CodeForces Solution


dp greedy sortings two pointers *1800

Please click on ads to support us..

C++ Code:

//i_love_aisukiuwu
#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
	#include "/home/trucmai/.vim/tools.h"
	#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
	#else
	#define debug(x...)
#endif

void open(){
  if(fopen("i.inp","r")){
    freopen("i.inp","r",stdin);
    freopen("o.out","w",stdout);
  }
}

#define ll long long
#define all(a) (a).begin(), (a).end()
#define vi vector<ll>
#define pi pair<ll,ll>
#define pii pair<ll,pair<ll,ll>>
#define fi first
#define se second
#define gcd __gcd
#define mset(a,v) memset(a, v, sizeof(a))
#define endl '\n'
#define spc " "

const int MN1 = 1e6 + 5,MN2 = 1e4 + 5,LOG = 27;
const ll MOD = 1e9 + 7,INF = 1e9;
ll n,d,m,a[MN1];

void solve(){
  vector<ll> small = {0},large = {0};
  cin>>n>>d>>m;
  for(ll i = 1;i <= n;++i){
    cin>>a[i];
    if(a[i] <= m) small.push_back(a[i]);
    else large.push_back(a[i]);
  }
  sort(small.rbegin(),small.rend()-1); 
  sort(large.rbegin(),large.rend()-1);
  ll res = 0;
  partial_sum(all(small),small.begin());
  partial_sum(all(large),large.begin());
  for(ll i = 0;i < large.size();++i){
    ll take = (i ? (i-1)*d + i : 0),
       tmp = n - take; 
    if(tmp < 0) continue;
    res = max(res,large[i] + small[min(tmp,(ll)small.size()-1)]);
  } 
  cout<<res;
}

signed main(){
  cin.tie(0) -> sync_with_stdio(0);
  open();
  ll t = 1; //cin>>t;
  while(t--) solve(); 
  
  #ifdef LOCAL
    cerr << endl << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  #endif
}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations